#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/8/18 16:59
# ===========================================
#       题目名称： 14. 最长公共前缀
#       题目地址： https://leetcode.cn/problems/longest-common-prefix/
#       题目描述： https://note.youdao.com/s/RRXNSXjR
# ===========================================


class Solution:
    """
        实现思路
            循环遍历
    """

    def longestCommonPrefix(self, strs):
        prefix = ""
        for i in range(len(strs)):
            s = strs[i]
            if prefix:
                for j in range(len(prefix)):
                    if j >= len(s) or prefix[j] != s[j]:
                        prefix = prefix[:j]
                        break
            elif i is 0:
                prefix = s
            else:
                break
        return prefix


if __name__ == '__main__':
    s = Solution()
    print("fl =>", s.longestCommonPrefix(strs=["flower", "flow", "flight"]))
    print("'' =>", s.longestCommonPrefix(strs=["dog", "racecar", "car"]))
